<?php
	require_once('../error_handler.php');
	require_once('../connect.php');
	
	$flag = $_GET['useGroups'];
	
	$mysql = connect();
	
	$sql = "SET NAMES 'utf8'";
	$mysql->query($sql);
	
	if ($flag)	
		$query = "SELECT `Users`.`NAME`, `Users`.`GID`, `Users`.`ID`, `Groups`.`GROUPNAME`, `Users`.`NUMBER`, `Users`.`MOBILE`
				  FROM `Groups`, `Users`
				  WHERE `Users`.`GID` = `Groups`.`GID`
				  ORDER BY `Groups`.`GROUPNAME`,`Users`.`NAME` ASC";		
	else
		$query = "select ID, NAME, NUMBER, MOBILE from `Users` where ID IS NOT NULL ORDER BY NAME";
			
    $result = $mysql->query($query);
	
    if (!$result)
	{
		echo 'Ошибка соединения. Пожалуйста попробуйте позже.';
        exit;		                
	}
	
	header('Content-Type: text/xml');
	$dom = new DOMDocument();
	
	$response = $dom->createElement('response');
	$dom->appendChild($response);
	
	if ($flag)
	{
		while ($row = $result->fetch_array(MYSQLI_ASSOC))
		{
			$userNumber = $dom->createElement('number');
			$userNumberText = $dom->createTextNode($row['NUMBER']);
			$userNumber->appendChild($userNumberText);

			$userMobile = $dom->createElement('mobile');
			$userMobileText = $dom->createTextNode($row['MOBILE']);
			$userMobile->appendChild($userMobileText);
			
			$user = $dom->createElement('user');
			
			$userNameAttr = $dom->createAttribute('name');
			$userNameText = $dom->createTextNode($row['NAME']);
			$userNameAttr->appendChild($userNameText);
			
			$userIdAttr = $dom->createAttribute('id');
			$userIdText = $dom->createTextNode($row['ID']);
			$userIdAttr->appendChild($userIdText);
			
			$user->appendChild($userNameAttr);
			$user->appendChild($userIdAttr);
			$user->appendChild($userNumber);
			$user->appendChild($userMobile);

			
			if (!isset($groupFlag))
			{
				$groupFlag = $row['GROUPNAME'];
			}
			
			if ($groupFlag == $row['GROUPNAME'])
			{				
				if ($dom->getElementsByTagName('group')->length != 0)
					$userGroup->appendChild($user);			
				else
				{
					$userGroup = $dom->createElement('group');
					
					$userGroupAttr = $dom->createAttribute('groupname');
					$userGroupText = $dom->createTextNode($row['GROUPNAME']);
					$userGroupAttr->appendChild($userGroupText);
					
					$userGidAttr = $dom->createAttribute('gid');
					$userGidText = $dom->createTextNode($row['GID']);
					$userGidAttr->appendChild($userGidText);
					
					$userGroup->appendChild($userGroupAttr);
					$userGroup->appendChild($userGidAttr);
					$userGroup->appendChild($user);
				}				
			}
			else
			{
				$userGroup = $dom->createElement('group');
				$userGroupAttr = $dom->createAttribute('groupname');
				$userGroupText = $dom->createTextNode($row['GROUPNAME']);
				
				$userGidAttr = $dom->createAttribute('gid');
				$userGidText = $dom->createTextNode($row['GID']);
				$userGidAttr->appendChild($userGidText);
					
				$userGroupAttr->appendChild($userGroupText);
				$userGroup->appendChild($userGroupAttr);
				$userGroup->appendChild($userGidAttr);
				$userGroup->appendChild($user);
				
				$groupFlag = $row['GROUPNAME'];				
			}
			
			$response->appendChild($userGroup);
		}		
	}
	else
	{
		while ($row = $result->fetch_array(MYSQLI_ASSOC))
		{
			$userNumber = $dom->createElement('number');
			$userNumberText = $dom->createTextNode($row['NUMBER']);
			$userNumber->appendChild($userNumberText);

			$userMobile = $dom->createElement('mobile');
			$userMobileText = $dom->createTextNode($row['MOBILE']);
			$userMobile->appendChild($userMobileText);
			
			$user = $dom->createElement('user');
			
			$userNameAttr = $dom->createAttribute('name');
			$userNameText = $dom->createTextNode($row['NAME']);
			$userNameAttr->appendChild($userNameText);
			
			$userIdAttr = $dom->createAttribute('id');
			$userIdText = $dom->createTextNode($row['ID']);
			$userIdAttr->appendChild($userIdText);
			
			$user->appendChild($userNameAttr);		
			$user->appendChild($userIdAttr);
			$user->appendChild($userNumber);
			$user->appendChild($userMobile);

			$response->appendChild($user);
		}
	}
		
	$result->close();
	$mysql->close();
	
	$xmlString = $dom->saveXML();
	echo $xmlString;
?>